﻿Imports System.Data.OleDb
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Create a random number between 1 - 6 to choose a random sqlstring, each time page is loaded
        Dim rn As New Random
        Dim n As Integer = rn.Next(1, 6)
        getRandomPictures(n)
        If IsPostBack = False Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            Dim SqlString As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Home")
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub getRandomPictures(ByVal r As Integer)
        'Declaring an empty sqlstring
        Dim sqlString As String = ""

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)

        'Creating a switch in order to choose a sqlstring based on the random number
        Select Case (r)
            Case Is = 1
                sqlString = "SELECT TOP 3 ID, BannerPicture FROM Beaches ORDER BY ID DESC"
                lbl_Banner.Text = "Most recent additions"
            Case Is = 2
                sqlString = "SELECT TOP 3 AverageRating, BannerPicture FROM BeachesWithAverageRating ORDER BY AverageRating DESC"
                lbl_Banner.Text = "Highest rated beaches"
            Case Is = 3
                sqlString = "SELECT TOP 3 TotalLikes, BannerPicture FROM BeachesWithMostLikes ORDER BY TotalLikes DESC"
                lbl_Banner.Text = "Most liked beaches"
            Case Is = 4
                sqlString = "SELECT TOP 3 TotalComments, BannerPicture FROM BeachesWithMostComments ORDER BY TotalComments DESC"
                lbl_Banner.Text = "Most discussed beaches"
            Case Is = 5
                sqlString = "SELECT TOP 3 TotalViews, BannerPicture FROM BeachesWithMostViews ORDER BY TotalViews DESC"
                lbl_Banner.Text = "Most viewed beaches"
        End Select

        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        Dim myList As New List(Of String)

        While dr.Read()
            myList.Add(dr("BannerPicture"))
        End While
        'If list has less than 3 beaches to show, then a black picture is assigned to banner
        If myList.Count < 3 Then
            Image1.ImageUrl = "Images/blank.png"
            Image2.ImageUrl = "Images/blank.png"
            Image3.ImageUrl = "Images/blank.png"
        Else
            Image1.ImageUrl = myList(0)
            Image2.ImageUrl = myList(1)
            Image3.ImageUrl = myList(2)
        End If
        oleDbConn.Close()
    End Sub

End Class